using System;
using System.Collections.Generic;
using System.Text;

using Cards.Util;
using Cards.GUI;

namespace Cards.Objects
{
    public class CPackOfCards
    {
        private INS_PackOfCards m_insPC;
        private int[] m_cardsArray = new int[Constants.s_TotalOfCards];
        private int m_index;

        public INS_PackOfCards IPC
        {
            get { return m_insPC; }
            set { m_insPC = value; }
        }

        public int INDEX
        {
            get { return m_index; }
            set { m_index = value; }
        }

        public int[] CARDSARRAY
        {
            get { return m_cardsArray; }
            set { m_cardsArray = value; }
        }

        public int this[int index]
        {
            get
            {
                if ((index < 0) || (index > m_cardsArray.Length))
                {
                    return -1;
                }
                else
                {
                    return m_cardsArray[index];
                }
            }
            set
            {
                if (index >= 0 && index < m_cardsArray.Length)
                {
                    m_cardsArray[index] = value;
                }
            }
        }

        public CPackOfCards(INS_PackOfCards _insPC)
        {
            this.m_insPC = _insPC;
            m_index = 0;
        }

        public void Init()
        {
            m_cardsArray[0] = Constants.BABICH;
            m_cardsArray[1] = Constants.BACHUON;
            m_cardsArray[2] = Constants.BARO;
            m_cardsArray[3] = Constants.BACO;
            m_cardsArray[4] = Constants.BONBICH;
            m_cardsArray[5] = Constants.BONCHUON;
            m_cardsArray[6] = Constants.BONRO;
            m_cardsArray[7] = Constants.BONCO;
            m_cardsArray[8] = Constants.NAMBICH;
            m_cardsArray[9] = Constants.NAMCHUON;
            m_cardsArray[10] = Constants.NAMRO;
            m_cardsArray[11] = Constants.NAMCO;
            m_cardsArray[12] = Constants.SAUBICH;
            m_cardsArray[13] = Constants.SAUCHUON;
            m_cardsArray[14] = Constants.SAURO;
            m_cardsArray[15] = Constants.SAUCO;
            m_cardsArray[16] = Constants.BAYBICH;
            m_cardsArray[17] = Constants.BAYCHUON;
            m_cardsArray[18] = Constants.BAYRO;
            m_cardsArray[19] = Constants.BAYCO;
            m_cardsArray[20] = Constants.TAMBICH;
            m_cardsArray[21] = Constants.TAMCHUON;
            m_cardsArray[22] = Constants.TAMRO;
            m_cardsArray[23] = Constants.TAMCO;
            m_cardsArray[24] = Constants.CHINBICH;
            m_cardsArray[25] = Constants.CHINCHUON;
            m_cardsArray[26] = Constants.CHINRO;
            m_cardsArray[27] = Constants.CHINCO;
            m_cardsArray[28] = Constants.MUOIBICH;
            m_cardsArray[29] = Constants.MUOICHUON;
            m_cardsArray[30] = Constants.MUOIRO;
            m_cardsArray[31] = Constants.MUOICO;
            m_cardsArray[32] = Constants.BOIBICH;
            m_cardsArray[33] = Constants.BOICHUON;
            m_cardsArray[34] = Constants.BOIRO;
            m_cardsArray[35] = Constants.BOICO;
            m_cardsArray[36] = Constants.DAMBICH;
            m_cardsArray[37] = Constants.DAMCHUON;
            m_cardsArray[38] = Constants.DAMRO;
            m_cardsArray[39] = Constants.DAMCO;
            m_cardsArray[40] = Constants.GIABICH;
            m_cardsArray[41] = Constants.GIACHUON;
            m_cardsArray[42] = Constants.GIARO;
            m_cardsArray[43] = Constants.GIACO;
            m_cardsArray[44] = Constants.ACHBICH;
            m_cardsArray[45] = Constants.ACHCHUON;
            m_cardsArray[46] = Constants.ACHRO;
            m_cardsArray[47] = Constants.ACHCO;
            m_cardsArray[48] = Constants.HAIBICH;
            m_cardsArray[49] = Constants.HAICHUON;
            m_cardsArray[50] = Constants.HAIRO;
            m_cardsArray[51] = Constants.HAICO;
        }

        public void MergeCards()
        {
            Random rd = new Random();
            int idx1 = 0, idx2 = 1;
            for (int i = 0; i < 1200; i++)
            {
                idx1 = rd.Next(0, 52);
                idx2 = rd.Next(0, 52);
                ArrayUtil.SwapArrayElements(m_cardsArray, idx1, idx2);
            }

            m_index = 0;
        }

        public void DistributeVNPockerCards(INS_Card[] _arr1, INS_Card[] _arr2, INS_Card[] _arr3, INS_Card[] _arr4)
        {
            int i = 0;
            int j = 0;
            while (i < m_cardsArray.Length)
            {
                _arr1[j].CARD.CARDTYPE   = m_cardsArray[i++];
                _arr1[j].CARD.CreateCardValue();
                _arr2[j].CARD.CARDTYPE   = m_cardsArray[i++];
                _arr2[j].CARD.CreateCardValue();
                _arr3[j].CARD.CARDTYPE   = m_cardsArray[i++];
                _arr3[j].CARD.CreateCardValue();
                _arr4[j].CARD.CARDTYPE = m_cardsArray[i++];
                _arr4[j].CARD.CreateCardValue();

                j++;
            }
        }

        public void DistributeVNPockerCards(INS_Stage stage)
        {
            int i = 0; // index bo bai
            int j = 0; // index mang quan bai nguoi choi
            int k = 0; // index nguoi choi

            while ((i < m_cardsArray.Length) && (j < Constants.VNPOCKER_NUMOFCARDS))
            {
                for (k = 0; k < stage.NUMPLAYERS; k++)
                {
                    stage[k].IC[j].CARD.CARDTYPE = m_cardsArray[i++];
                    stage[k].IC[j].CARD.CreateCardValue();
                }
                j++;
            }

            m_index = i;
        }

        public void DistributeBlackJackCards(INS_Stage stage)
        {
            int i = 0; // index bo bai
            int j = 0; // index mang quan bai nguoi choi
            int k = 0; // index nguoi choi

            while ((i < m_cardsArray.Length) && (j < Constants.BLACKJACK_NUMOFCARDS))
            {
                for (k = 0; k < stage.NUMPLAYERS; k++)
                {
                    stage[k].IC[j].CARD.CARDTYPE = m_cardsArray[i++];
                    stage[k].IC[j].CARD.CreateCardValue();
                    ((INS_BlackJackPlayer)stage[k]).INDEX = j+1;
                }
                j++;
            }

            m_index = i;
        }

        public void DistributeCatteCards()
        {
        }

        public String ToCommandMessageString()
        {
            String str = "";

            for (int i = 0; i < Constants.s_TotalOfCards; i++)
            {
                if (i > 0)
                {
                    str += NetworkConstants.MESSAGETYPE_SUBSEPARATOR;
                }
                str += m_cardsArray[i];
            }

            str += NetworkConstants.MESSAGETYPE_SEPARATOR;
            str += m_index;

            return str;
        }
    }
}
